<template>
	<view>
		<view class="search-box">
			<uni-search-bar @confirm="comfirm" @input="input" :radius="100" placeholder="自定placeholder"
				cancelButton=:none clearButton="auto"></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="suggests-list" v-if="searchList.length!==0">
			<view class="suggests-item" v-for="item,index in searchList" :key="index" @click="toGoodsDetail(item)">
				<view class="goods-description">
					{{item.goods_name}}
				</view>
				<uni-icons type="forward" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="20" @click="clean"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="item,index in histories" :key="index" @click="toGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				keyword: '',
				searchList: [],
				historyList: []
			};
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keyword = e
					this.getSearchList()
				}, 500)
			},
			comfirm() {

				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + this.keyword
				})

				this.saveHistoryList()
			},
			async getSearchList() {
				if (this.keyword.trim().length === 0) {
					this.searchList = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.keyword
				})
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.searchList = res.message
			},
			toGoodsDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
				this.saveHistoryList()
			},
			saveHistoryList() {
				const set = new Set(this.historyList)
				set.delete(this.keyword)
				set.add(this.keyword)
				this.historyList = Array.from(set)

				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			toGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.suggests-list {
		padding: 0 5px;

		.suggests-item {
			padding: 13px 0;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #eeeeee;
		}

		.goods-description {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			font-size: 14px;
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 14px;
			border-bottom: 1px solid #eeeeee;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>
